global class ActivityRemandByEmail implements schedulable
{
      global void execute(schedulablecontext sc)
     {
         //查出所有活动，满足以下条件：1，今天距离活动开始至少有21天；2，总部已提交活动，3，活动状态正常
         CommonMethod getCommonMethod=new CommonMethod();
         List<Activity_Information__c> ActivityInitList=new List<Activity_Information__c>();
          for(Activity_Information__c ActivityInfo:[Select Id,Name, RecordTypeId, ASMConfirmDate__c, ASMFinishDate__c, Activity_End_Date__c,
                                  Activity_Notice__c, Activity_Stage__c, Activity_Start_Date__c, Activity_Type_State__c, Activity_Type__c, 
                                  KAMConfirmDate__c, KAMFinishAdjustDate__c, NTMKTFinishApproveDate__c, RSMFinishApproveDate__c, 
                                  RTMFinishApproveDate__c, SRTMFinishApproveDate__c, SRTMFinishDate__c,Serial_Number__c,Regional_Activity_Type__c
                                   FROM Activity_Information__c  where Activity_Type_State__c='正常' and (Activity_Stage__c='总部已发起活动' 
                                   or Activity_Stage__c='SRTM已发起活动' or Activity_Stage__c='SRTM已完成区域调整')
                                   ]){
                  Date StartDT=ActivityInfo.Activity_Start_Date__c;                   
                  if(Date.today()< StartDT.addDays(-21)){   //开始活动前21天
                  //  Region ASM提交
                     if((ActivityInfo.Activity_Type__c=='Region' && ActivityInfo.Activity_Stage__c=='SRTM已完成区域调整') || (ActivityInfo.Activity_Type__c=='Region' && ActivityInfo.Regional_Activity_Type__c=='竞品活动')){
                        if(Date.today()>=ActivityInfo.ASMFinishDate__c.addDays(-2) && Date.today()<=ActivityInfo.ASMFinishDate__c){  
                           List<ParticipatorArea__c> paList=[select Area__c from ParticipatorArea__c where Activity_Name__c=:ActivityInfo.id];
                           Set<String> areaList=new Set<String>();
                           for(ParticipatorArea__c p:paList){
                             areaList.add(p.Area__c);
                           }
                          getCommonMethod.SendEmailToASM(ActivityInfo,areaList);
                        }
                      }
                      //Region(Remind) ASM提交
                        if(ActivityInfo.Activity_Type__c=='Region(剩余资源)'){
                        if(Date.today()>=ActivityInfo.ASMFinishDate__c.addDays(-2) && Date.today()<=ActivityInfo.ASMFinishDate__c){  
                           List<Participator_Detail_Outlet__c> paList=[select Area__c from Participator_Detail_Outlet__c where Participator_Area__r.Activity_Name__c=:ActivityInfo.id];
                           Set<String> areaList=new Set<String>();
                           for(Participator_Detail_Outlet__c p:paList){
                             areaList.add(p.Area__c);
                           }
                          getCommonMethod.SendEmailToASM(ActivityInfo,areaList);
                        }
                      }
                     //1.SSY和KA活动判断KAMFinishAdjustDate__c，是否发送邮件通知
                     if(ActivityInfo.Activity_Type__c=='KA' || ActivityInfo.Activity_Type__c=='SSY' ){
                         if(Date.today()>= ActivityInfo.KAMFinishAdjustDate__c.addDays(-2) && Date.today()<= ActivityInfo.KAMFinishAdjustDate__c){  
                         //发送邮件给KAM
                           List<ParticipatorArea__c> paList=[select KA__c from ParticipatorArea__c where Activity_Name__c=:ActivityInfo.id and  KA__c!=NULL and KA_Status__c!='已审批' and KA_Status__c!='KAM已提交'];
                           if(paList!=NULL && paList.size()>0){
                              String BodyString='【'+ActivityInfo.Name+'】活动距离提交时间即将结束,请尽快登陆凤凰系统进行提交,谢谢！';
                              String subject='确认【'+ActivityInfo.Name+'】活动时间';
                              EmailHander.SendMail(BodyString,getCommonMethod.getEmail(paList),subject,'',null);
                          }
                         }
                         //2.SSY，KA判断总部是否在总部完成审批时间前2天是否已审批
                         if(Date.today()>= ActivityInfo.NTMKTFinishApproveDate__c.addDays(-2) && Date.today()<= ActivityInfo.NTMKTFinishApproveDate__c){  
                         //发送邮件给KAM
                           List<ParticipatorArea__c> paList=[select KA__c from ParticipatorArea__c where Activity_Name__c=:ActivityInfo.id and  KA__c!=NULL and  KA_Status__c='KAM已提交'];
                           if(paList!=NULL && paList.size()>0){
                             String BodyString='【'+ActivityInfo.Name+'】活动距离审批时间即将结束,请尽快登陆凤凰系统进行审批,谢谢！';
                             String subject='确认【'+ActivityInfo.Name+'】活动时间';
                             EmailHander.SendMail(BodyString,getCommonMethod.getUserEmailAddress('NTMKT',ActivityInfo.id),subject,'',null);
                           }
                         }
                         //3，SSY活动，邮件通知SRTM进行资源调整
                         if(ActivityInfo.Activity_Type__c=='SSY' ){
                              if(Date.today()>= ActivityInfo.SRTMFinishDate__c.addDays(-2) && Date.today()<= ActivityInfo.SRTMFinishDate__c){  
                                 List<ParticipatorArea__c> paList=[select KA__c,Region__c from ParticipatorArea__c where Activity_Name__c=:ActivityInfo.id and  OT_Status__c=NULL and KA_Status__c='已审批'];
                                 if(paList!=null && paList.size()>0){
                                    Set<string> regionList=new Set<string>();
                                    List<string> addressList=new List<string>();
                                    for(ParticipatorArea__c area:paList)
                                      regionList.add(area.Region__c);
                                    for(User u:[select Email from User where UserRegion__c in: regionList and Title_Functional__c='SRTM'])
                                     addressList.add(u.Email);   
                                  String BodyString='【'+ActivityInfo.Name+'】活动距离SRTM提交时间即将结束,请尽快登陆凤凰系统进行提交,谢谢！';
                                  String subject='调整提交【'+ActivityInfo.Name+'】活动资源';
                                  EmailHander.SendMail(BodyString,addressList,subject,'',null);                     
                                 }
                                       
                              }
                              //SSYASM提交
                                 if(Date.today()>=ActivityInfo.ASMFinishDate__c.addDays(-2) && Date.today()<=ActivityInfo.ASMFinishDate__c){  
                                   List<ParticipatorArea__c> paList=[select Area__c from ParticipatorArea__c where Activity_Name__c=:ActivityInfo.id and OT_Status__c='SRTM已提交'];
                                  Set<String> areaList=new Set<String>();
                                  for(ParticipatorArea__c p:paList){
                                     areaList.add(p.Area__c);
                                   }
                                   getCommonMethod.SendEmailToASM(ActivityInfo,areaList);
                                 }
                         }
                          //3，
                         if(ActivityInfo.Activity_Type__c=='KA' ){
                          //  KA ASM提交
                               if(Date.today()>=ActivityInfo.ASMFinishDate__c.addDays(-2) && Date.today()<=ActivityInfo.ASMFinishDate__c){  
                                   List<ParticipatorArea__c> paList=[select Area__c from ParticipatorArea__c where Activity_Name__c=:ActivityInfo.id and KA_Status__c='已审批'];
                                   Set<String> areaList=new Set<String>();
                                   for(ParticipatorArea__c p:paList){
                                     areaList.add(p.Area__c);
                                   }
                                   getCommonMethod.SendEmailToASM(ActivityInfo,areaList);
                                 }
                             // KA ASM时间确认   
                             if(Date.today()>= ActivityInfo.ASMConfirmDate__c.addDays(-2) && Date.today()<= ActivityInfo.ASMConfirmDate__c){  
                                List<String> emailList=new List<String>();
                                for(Participator_Detail_Area__c pda:[select Area__c,Region__c,Submit_ASM__r.Email from Participator_Detail_Area__c where Activity_Information__c=:ActivityInfo.id and IsActive__c=true and Approve_Type__c='ASM已提交']){
                                    emailList.add(pda.Submit_ASM__r.Email);                     
                                 }
                                String BodyString='【'+ActivityInfo.Name+'】活动距离ASM确认时间即将结束,请尽快登陆凤凰系统进行确认,谢谢！';
                                String subject='确认【'+ActivityInfo.Name+'】活动时间';
                                EmailHander.SendMail(BodyString,emailList,subject,'',null);       
                             }
                         }
                     }
                 }    
               //发送至SRTM
               /*
             if(ActivityInfo.Activity_Type__c=='Region' &&  ActivityInfo.Activity_Stage__c=='SRTM已发起活动'){
                 
              }*/
               //SSY，Region，Region剩余活动RSM，RTM，SRTM审批时间提醒邮件
            if(ActivityInfo.Activity_Type__c=='Region' || ActivityInfo.Activity_Type__c=='Region(剩余资源)' || ActivityInfo.Activity_Type__c=='SSY'){
              //RSM
                if(Date.today()>= ActivityInfo.RSMFinishApproveDate__c.addDays(-2) && Date.today()<= ActivityInfo.RSMFinishApproveDate__c){  
                    List<String> emailList=new List<String>();
                    Set<String> idList=new Set<String>();
                    for(Participator_Detail_Area__c pda:[select Area__c,Submit_ASM__r.ManagerId from Participator_Detail_Area__c where Activity_Information__c=:ActivityInfo.id and IsActive__c=true and Approve_Type__c='ASM已提交']){
                        idList.add(pda.Submit_ASM__r.ManagerId);                     
                    }
                    for(User u:[select id,Email from User where id=:idList]){
                        emailList.add(u.Email);                     
                    }
                     String BodyString='【'+ActivityInfo.Name+'】活动距离RSM审批时间即将结束,请尽快登陆凤凰系统进行审批,谢谢！';
                     String subject='审批【'+ActivityInfo.Name+'】活动';
                     EmailHander.SendMail(BodyString,emailList,subject,'',null);       
                }
                
                //RTM
                  if(Date.today()>= ActivityInfo.RTMFinishApproveDate__c.addDays(-2) && Date.today()<= ActivityInfo.RTMFinishApproveDate__c){  
                    Set<String> SubRegionList=new Set<String>();
                    List<String> emailList=new List<String>();
                    for(Participator_Detail_Area__c pda:[select Area__c,SubRegion__c from Participator_Detail_Area__c where Activity_Information__c=:ActivityInfo.id and IsActive__c=true and Approve_Type__c='RSM已审批' and Approve_Description__c='同意']){
                        SubRegionList.add(pda.SubRegion__c);                     
                    }
                    for(User u:[select id,Email from User where UserSubRegion__c=:SubRegionList and Title_Functional__c='RTM']){
                        emailList.add(u.Email);                     
                    }
                     String BodyString='【'+ActivityInfo.Name+'】活动距离RTM审批时间即将结束,请尽快登陆凤凰系统进行审批,谢谢！';
                     String subject='审批【'+ActivityInfo.Name+'】活动';
                     EmailHander.SendMail(BodyString,emailList,subject,'',null);       
                }
                  //SRTM
                  if(Date.today()>= ActivityInfo.SRTMFinishApproveDate__c.addDays(-2) && Date.today()<= ActivityInfo.SRTMFinishApproveDate__c){  
                    Set<String> RegionList=new Set<String>();
                    List<String> emailList=new List<String>();
                    for(Participator_Detail_Area__c pda:[select Area__c,Region__c from Participator_Detail_Area__c where Activity_Information__c=:ActivityInfo.id and IsActive__c=true and (Approve_Type__c='RTM已审批' or Approve_Type__c='RSM已审批') and Approve_Description__c='同意']){
                        RegionList.add(pda.Region__c);                     
                    }
                    for(User u:[select id,Email from User where UserRegion__c=:RegionList and Title_Functional__c='SRTM']){
                        emailList.add(u.Email);                     
                    }
                     String BodyString='【'+ActivityInfo.Name+'】活动距离SRTM审批时间即将结束,请尽快登陆凤凰系统进行审批,谢谢！';
                     String subject='审批【'+ActivityInfo.Name+'】活动';
                     EmailHander.SendMail(BodyString,emailList,subject,'',null);       
                }
                 //ASM确认时间
                  if(Date.today()>= ActivityInfo.ASMConfirmDate__c.addDays(-2) && Date.today()<= ActivityInfo.ASMConfirmDate__c){  
                    List<String> emailList=new List<String>();
                    for(Participator_Detail_Area__c pda:[select Area__c,Region__c,Submit_ASM__r.Email from Participator_Detail_Area__c where Activity_Information__c=:ActivityInfo.id and IsActive__c=true and Approve_Type__c='SRTM已审批' and Approve_Description__c='同意']){
                        emailList.add(pda.Submit_ASM__r.Email);                     
                    }
                     String BodyString='【'+ActivityInfo.Name+'】活动距离ASM确认时间即将结束,请尽快登陆凤凰系统进行确认,谢谢！';
                     String subject='确认【'+ActivityInfo.Name+'】活动时间';
                     EmailHander.SendMail(BodyString,emailList,subject,'',null);       
                }
                
                //活动开始前一周发送消息给TSR
              if(Date.today()< StartDT.addDays(-7)){  
                Message__c msg=new Message__c();
                msg.Content__c='【'+ActivityInfo.Name+'】活动还有一周时间开始，请尽快登陆凤凰系统查看活动详细信息！';
                msg.StartDate__c=StartDT.addDays(-7);
                msg.EndDate__c=StartDT;
                msg.Type__c='营业信息';
                msg.Name='【'+ActivityInfo.Name+'】活动消息提醒';
                
                Set<ID> userSet=new Set<ID>();
                for(Participator_Detail_Outlet__c p:[select OwnerId from Participator_Detail_Outlet__c where ActivityId__c=:String.valueOf(ActivityInfo.id).substring(0,15)]){
                  userSet.add(p.OwnerId);
                }
               
                if(userSet!=null && userSet.size()>0)
                      insert msg;
                      
                List<MessageRecipient__c> mrList=new List<MessageRecipient__c>();
                for(ID idStr:userSet){
                   MessageRecipient__c mr=new MessageRecipient__c(Recipient__c=idStr,Message__c=msg.id);                 
                  mrList.add(mr);
                }    
                insert mrList;
              
               }
              }
              //
              
           }       
      }
                               
  
  
}